//
//  RequestManager.m
//  Lianxi2
//
//  Created by lanou on 16/7/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "RequestManager.h"




@implementation RequestManager

+ (void)fetchDataWithURL:(NSString *)url parameter:(NSDictionary *)dic metod:(NSString *)method compelet:(RequestCompeled)compele updateUI:(UpdateUI)update{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //创建子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        
        if ([method isEqualToString:@"GET"]) {
            
            [manager GET:url parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
               
                //把Data传出去
                compele((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    update();
                });
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"获取失败");
            }];
            
        }else{
            
            [manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                //把Data传出去
                compele((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    update();
                });
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"获取失败");
            }];
            
        }
        
        
        
    });
    
    
    
    
    
}



@end
